package com.stylefeng.guns.modular.salesmanagement.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.GoodsSaleAddRequest;
import com.stylefeng.guns.modular.dto.SaleGoodsQueryRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.GoodsSale;
import com.stylefeng.guns.modular.salesmanagement.service.IGoodsSaleService;

import java.util.Date;

/**
 * 销售出库控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 21:16:48
 */
@Controller
@RequestMapping("/goodsSale")
public class GoodsSaleController extends BaseController {

    private String PREFIX = "/salesmanagement/goodsSale/";

    @Autowired
    private IGoodsSaleService goodsSaleService;

    /**
     * 跳转到销售出库首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String orderNumber = "XS" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("saleOrderNumber", orderNumber);
        return PREFIX + "goodsSale.html";
    }

    /**
     * 跳转到添加销售出库
     */
    @RequestMapping("/goodsSale_add")
    public String goodsSaleAdd() {
        return PREFIX + "goodsSale_add.html";
    }

    /**
     * 跳转到修改销售出库
     */
    @RequestMapping("/goodsSale_update/{goodsSaleId}")
    public String goodsSaleUpdate(@PathVariable Integer goodsSaleId, Model model) {
        GoodsSale goodsSale = goodsSaleService.selectById(goodsSaleId);
        model.addAttribute("item",goodsSale);
        LogObjectHolder.me().set(goodsSale);
        return PREFIX + "goodsSale_edit.html";
    }

    /**
     * 获取销售出库列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(SaleGoodsQueryRequest request) {
        Page<GoodsSale> page = new PageFactory<GoodsSale>().defaultPage();
        Page<GoodsSale> resultPage = goodsSaleService.selectPageWithDataScope(page,request);
        PageInfoBT<GoodsSale> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    /**
     * 新增销售出库
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody GoodsSaleAddRequest request) {
        boolean success = goodsSaleService.insertGoodSaleAndGoods(request);
        if(success){
            String orderNumber = "XS" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(orderNumber);
        }
        return new ErrorTip(500,"保存失败，请检查参数!");
    }

    /**
     * 删除销售出库
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "goodsSaleListId") Long goodsSaleListId) {
        boolean success = goodsSaleService.deleteGoodSaleAndGoods(goodsSaleListId);
        if(success){
            return SUCCESS_TIP;
        }
        return  new ErrorTip(500,"删除失败!请稍后再试.");
    }

    /**
     * 修改销售出库
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsSale goodsSale) {
        goodsSaleService.updateById(goodsSale);
        return SUCCESS_TIP;
    }

    /**
     * 销售出库详情
     */
    @RequestMapping(value = "/detail/{goodsSaleId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsSaleId") Integer goodsSaleId) {
        return goodsSaleService.selectById(goodsSaleId);
    }
}
